package kumi.controller;

import com.alibaba.fastjson.JSON;
import kumi.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static kumi.kumi.db.getConnection.getConnection;

/**
 * Created by Administrator on 2016/8/6 0006.
 */

@Controller
@RequestMapping(value = "/user")
public class Login2 {
    @RequestMapping(value="/denglu.do")
    @ResponseBody

    public String login1(String username,String password,String phonenumber) {
//        System.out.print(username);
        List<User> userList = select();
//        System.out.print(username);
        String a="2";
        for (User user : userList) {
//            System.out.print(user.getUsername());
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                a="1";
                break;
            }
            else {
                a="2";
            }

        }
        return JSON.toJSONString(a);

    }

    public static List<User> select(){

        String sql = "SELECT username,password ,phonenumber FROM myusers";
        List<User> userList = new ArrayList<User>();
        PreparedStatement preparedStatement;

        try {
            preparedStatement = getConnection().prepareStatement(sql);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                User loginUser = new User();

                loginUser.setUsername(resultSet.getString("username"));
                loginUser.setPassword(resultSet.getString("password"));

                userList.add(loginUser);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;

    }


}